package view;

import java.awt.*;
import javax.swing.*;

/**
 * Klasse FriendCellRenderer
 * 
 * @author Magnus Brieler
 */
public class FriendCellRenderer implements ListCellRenderer {

	@Override
	public Component getListCellRendererComponent(JList list, Object value,
			int index, boolean isSelected, boolean cellHasFocus) {
		model.Friendship fs = (model.Friendship) value;

		JPanel panel = new JPanel();
		JPanel status = new JPanel();
		JLabel userName = new JLabel();
		userName.setFont(new Font(Font.DIALOG, Font.BOLD, 14));
		JLabel statusMessage = new JLabel();
		panel.setLayout(new BorderLayout());
		panel.add(statusMessage, BorderLayout.CENTER);
		panel.add(userName, BorderLayout.NORTH);
		panel.add(status, BorderLayout.EAST);

		if (cellHasFocus || isSelected) {
			panel.setBackground(Color.LIGHT_GRAY);
			userName.setForeground(Color.BLACK);
			statusMessage.setForeground(Color.BLACK);
		} else {
			panel.setBackground(Color.WHITE);
			userName.setForeground(Color.BLACK);
			statusMessage.setForeground(Color.BLACK);
		}

		if (fs.getGood()) {
			userName.setText(fs.getFriend().getFirstName() + " "
					+ fs.getFriend().getLastName() + " (" + fs.getFriend().getUserName()
					+ ")");
		} else {
			userName.setText(fs.getFriend().getUserName());
		}

		status.setBackground(fs.getFriend().getStatus().getColor());
		if (fs.getFriend().getStatus() == model.User.OnlineStatus.OFFLINE
				|| fs.getFriend().getStatus() == model.User.OnlineStatus.SHOWASOFFLINE) {
			statusMessage.setFont(new Font(Font.DIALOG, Font.ITALIC, 12));
			statusMessage.setForeground(fs.getFriend().getStatus().getColor());
			statusMessage.setText("Zuletzt online: vor "
					+ model.Moment.getHighestValue(model.Moment.determineDifference(fs
							.getFriend().getLastOnline(), model.Moment.getCurrentMoment())));
		} else if (fs.getFriend().getStatusMessage().equals("")) {
			statusMessage.setFont(new Font(Font.DIALOG, Font.ITALIC, 12));
			statusMessage.setForeground(fs.getFriend().getStatus().getColor());
			statusMessage.setText(fs.getFriend().getStatus().getText());
		} else {
			statusMessage.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
			statusMessage.setForeground(Color.BLACK);
			statusMessage.setText(fs.getFriend().getStatusMessage());
		}

		return panel;
	}

}
